package com.heima.listen;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.constants.message.NewsUpOrDownConstants;
import com.heima.search.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ArticleUpOrDownListener {
    @Autowired
    ApArticleConfigService apArticleConfigService;
    @RabbitListener(queuesToDeclare = {
            @Queue(value = NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE)
    })
    public void newsUpHandler(String articleId){
        log.info("获取其到文章Id{}",articleId);
        try {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                                    .set(ApArticleConfig::getIsDown,false)
                                    .eq(ApArticleConfig::getArticleId,Long.valueOf(articleId)));

        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.error("article 处理失败，文章id{},需要人工，原因{}",articleId,e.getMessage());
        }
        log.info("上架文章Id{}完成",articleId);

    }

    @RabbitListener(queuesToDeclare = {
            @Queue(value = NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE)
    })
    public void newsDownHandler(String articleId){
        log.info("获取其到文章Id{}",articleId);
        try {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .set(ApArticleConfig::getIsDown,true)
                    .eq(ApArticleConfig::getArticleId,Long.valueOf(articleId)));
        } catch (NumberFormatException e) {

            log.error("article 处理失败，文章id{},需要人工，原因{}",articleId,e.getMessage());
            e.printStackTrace();
        }
        log.info("下架文章Id{}完成",articleId);

    }
}
